/**
 * Created by IntelliJ IDEA.
 * User: Slejus
 * Date: 13.10.18
 * Time: 13.40
 * To change this template use File | Settings | File Templates.
 */
function customWorkflow(wfId) {
    this.wfId = wfId;
    this.args = null;
    this.type = null;
    this.destination = null;
    this.template = null;
    this.date = null;
    this.dateInText = null;
    this.dateInDigits = null;
    this.dateTime = null;
    this.title = null;
}

customWorkflow.prototype.setProperties = function() {
    this.setTypeAndDestination(this.wfId);
    this.setCurrentDate();
    this.dateInDigits = this.getCurrentDateFormatted("digits");
    this.dateInText = this.getCurrentDateFormatted("text");
    this.setDateTime();
    //this.setArgs();
    //this.setTemplate();
}

customWorkflow.prototype.setTypeAndDestination = function(workflowId) {
    if (workflowId == "vacAppealSimple") {
        this.type = "Pra\u0161ymas";
        this.destination = "Kasmetini\u0173 atostog\u0173 pra\u0161ymai";
    } else if (workflowId == "orderPerSimple"){
        this.type = "\u012Esakymas";
        this.destination = "Kasmetini\u0173 atostog\u0173 \u012Fsakymai";
    }
    else {
        this.type = "Unknown";
        this.destination = "Unknown";
    }
}

customWorkflow.prototype.setCurrentDate = function() {
    var date = new Date();
    this.date =  date;
}

customWorkflow.prototype.getCurrentDateFormatted = function(format) {
    var fullDate, year, month, day  = "";
    var date = this.date;
    if (date != null) {
        year = date.getFullYear().toString();
        day = date.getDate().toString();
        if (format == "text") {
            month = this.getMonthInText(date.getMonth());
            fullDate = year + " m. " + month + " " + day + " d.";
        } else {
            month = this.getMonthInDoubleDigits(date.getMonth());
            fullDate = year + "-" + month + "-" + day;
        }
    }
    return fullDate;
}

customWorkflow.prototype.setDateTime = function() {
    if (this.date != null) {
        var hour = this.date.getHours().toString();
        var min = this.date.getMinutes().toString();
        var sec = this.date.getSeconds().toString();
        this.dateTime = hour + min + sec;
    }
}

customWorkflow.prototype.setArgs = function() {
    var params = new Array();
    params['appealDate'] = this.dateInText;

    if (typeof rccwf_personDivision != 'undefined'){if(rccwf_personDivision != "" && rccwf_personDivision != null){params['division'] = rccwf_personDivision;}else{params['division'] = null;}}
    if (typeof rccwf_personPosition != 'undefined'){if (rccwf_personPosition != null && rccwf_personPosition != ""){params['position'] = rccwf_personPosition;}else{params['position'] = null;}}
    if (typeof rccwf_personFullName != 'undefined'){if (rccwf_personFullName != null && rccwf_personFullName != ""){params['fullName'] = rccwf_personFullName;}else{params['fullName'] = null;}}
    if (typeof rccwf_wfTitle != 'undefined'){if (rccwf_wfTitle != null && rccwf_wfTitle != ""){params['wfTitle'] = rccwf_wfTitle;}else {params['wfTitle'] = null;}}
    if (typeof rccwf_text != 'undefined'){if (rccwf_text != null && rccwf_text != ""){params['text'] = rccwf_text;}else{params['text'] = null;}}
    if (typeof rccwf_attachmentText != 'undefined'){if (rccwf_attachmentText != null && rccwf_attachmentText != ""){params["attachmentText"] = rccwf_attachmentText;}else{params["attachmentText"] = null;}}
    if (typeof rccwf_dateIntervalOneFrom != 'undefined'){if (rccwf_dateIntervalOneFrom != null && rccwf_dateIntervalOneFrom != ""){params["intervalOneFrom"] = rccwf_dateIntervalOneFrom;}else{params["intervalOneFrom"] = null;}}
    if (typeof rccwf_dateIntervalOneTo != 'undefined'){if (rccwf_dateIntervalOneTo != null && rccwf_dateIntervalOneTo != ""){params["intervalOneTo"] = rccwf_dateIntervalOneTo;}else{params["intervalOneTo"] = null;}}
    if (typeof rccwf_dateIntervalTwoFrom != 'undefined'){if (rccwf_dateIntervalTwoFrom != null && rccwf_dateIntervalTwoFrom != ""){params["intervalTwoFrom"] = rccwf_dateIntervalTwoFrom;}else{params["intervalTwoFrom"] = null;}}
    if (typeof rccwf_dateIntervalTwoTo != 'undefined'){if (rccwf_dateIntervalTwoTo != null && rccwf_dateIntervalTwoTo != ""){params["intervalTwoTo"] = rccwf_dateIntervalTwoTo;}else {params["intervalTwoTo"] = null;}}
    if (typeof rccwf_oneDate != 'undefined'){if (rccwf_oneDate != null && rccwf_oneDate != ""){params['oneDate'] = rccwf_oneDate;}else {params['oneDate'] = null;}}
    if (typeof rccwf_otherOption != 'undefined'){if (rccwf_otherOption != null && rccwf_otherOption != ""){params['otherOption'] = rccwf_otherOption;}else{params['otherOption'] = null;}}

    this.args = params;
}

customWorkflow.prototype.setOrderPerArgs = function() {
    var oParams = new Array();

    if (typeof task.getVariable('rccwf_employeDivision') != undefined) {var employeDivision = task.getVariable('rccwf_employeDivision'); oParams['employeDivision'] = employeDivision;}else{oParams['employeDivision']=null}
    if (typeof task.getVariable('rccwf_employePosition') != undefined) {var employePosition = task.getVariable('rccwf_employePosition'); oParams['employePosition'] = employePosition;}else{oParams['employePosition']=null}
    if (typeof task.getVariable('rccwf_employeFullName') != undefined) {var employeFullName = task.getVariable('rccwf_employeFullName'); oParams['employeFullName'] = employeFullName;}else{oParams['employeFullName']=null}
    if (typeof task.getVariable('rccwf_dateIntervalOneFromMan') != undefined) {var intervalOneFrom = task.getVariable('rccwf_dateIntervalOneFromMan'); oParams['dateIntervalOneFrom'] = intervalOneFrom;}else{oParams['dateIntervalOneFrom']=null}
    if (typeof task.getVariable('rccwf_dateIntervalOneToMan') != undefined) {var intervalOneTo = task.getVariable('rccwf_dateIntervalOneToMan'); oParams['dateIntervalOneTo'] = intervalOneTo;}else{oParams['dateIntervalOneTo']=null}
    if (typeof task.getVariable('rccwf_dateIntervalTwoFromMan') != undefined) {var intervalTwoFrom = task.getVariable('rccwf_dateIntervalTwoFromMan'); oParams['dateIntervalTwoFrom'] = intervalTwoFrom;}else{oParams['dateIntervalTwoFrom']=null}
    if (typeof task.getVariable('rccwf_dateIntervalTwoToMan') != undefined) {var intervalTwoTo = task.getVariable('rccwf_dateIntervalTwoToMan'); oParams['dateIntervalTwoTo'] = intervalTwoTo;}else{oParams['dateIntervalTwoTo']=null}
    if (typeof task.getVariable('rccwf_textMan') != undefined) {var text = task.getVariable('rccwf_textMan'); oParams['text'] = text;}

    if (typeof task.getVariable('rccwf_orderAdditionalDaysOne') != undefined) {var orderAdditionalDaysOne = task.getVariable('rccwf_orderAdditionalDaysOne'); oParams['orderAdditionalDaysOne'] = orderAdditionalDaysOne;}else{oParams['orderAdditionalDaysOne']=null}
    if (typeof task.getVariable('rccwf_orderAdditionalPeriodOneFrom') != undefined) {var orderAdditionalPeriodOneFrom = task.getVariable('rccwf_orderAdditionalPeriodOneFrom'); oParams['orderAdditionalPeriodOneFrom'] = orderAdditionalPeriodOneFrom;}else{oParams['orderAdditionalPeriodOneFrom']=null}
    if (typeof task.getVariable('rccwf_orderAdditionalPeriodOneTo') != undefined) {var orderAdditionalPeriodOneTo = task.getVariable('rccwf_orderAdditionalPeriodOneTo'); oParams['orderAdditionalPeriodOneTo'] = orderAdditionalPeriodOneTo;}else{oParams['orderAdditionalPeriodOneTo']=null}

    if (typeof task.getVariable('rccwf_orderAdditionalPeriodTwoFrom') != undefined) {var orderAdditionalPeriodTwoFrom = task.getVariable('rccwf_orderAdditionalPeriodTwoFrom'); oParams['orderAdditionalPeriodTwoFrom'] = orderAdditionalPeriodTwoFrom;}else{oParams['orderAdditionalPeriodTwoFrom']=null}
    if (typeof task.getVariable('rccwf_orderAdditionalPeriodTwoTo') != undefined) {var orderAdditionalPeriodTwoTo = task.getVariable('rccwf_orderAdditionalPeriodTwoTo'); oParams['orderAdditionalPeriodTwoTo'] = orderAdditionalPeriodTwoTo;}else{oParams['orderAdditionalPeriodTwoTo']=null}
    if (typeof task.getVariable('rccwf_orderAdditionalDaysTwo') != undefined) {var orderAdditionalDaysTwo = task.getVariable('rccwf_orderAdditionalDaysTwo'); oParams['orderAdditionalDaysTwo'] = orderAdditionalDaysTwo;}else{oParams['orderAdditionalDaysTwo']=null}

    if (typeof task.getVariable('rccwf_nextAssignee') != undefined) {var assigneeName = task.getVariable('rccwf_nextAssignee'); oParams['assigneeName'] = assigneeName.properties.firstName + " "+ assigneeName.properties.lastName;}else{oParams['assigneeName']=null}
    if (typeof task.getVariable('rccwf_position') != undefined) {var position = task.getVariable('rccwf_position'); oParams['position'] = position;}else{oParams['position']=null}

    if (typeof task.getVariable('rccwf_testing') != undefined) {var testing = task.getVariable('rccwf_testing'); oParams['testing'] = testing;}else{oParams['testing']=null}


    logger.log("Division: " + employeDivision);
    logger.log("Position: " + employePosition);
    logger.log("FullName: " + employeFullName);
    logger.log("intervalOneFrom: " + intervalOneFrom);
    logger.log("intervalOneTo: " + intervalOneTo);
    logger.log("intervalTwoFrom: " + intervalTwoFrom);
    logger.log("intervalTwoTo: " + intervalTwoTo);
    logger.log("text: " + text);
    logger.log("assigneeName: " + assigneeName);
    logger.log("position: " + position);
    logger.log("rccwf_orderAdditionalDaysOne: " + oParams['orderAdditionalDaysOne']);
    logger.log("rccwf_orderAdditionalDaysTwo: " + oParams['orderAdditionalDaysTwo']);

    logger.log("rccwf_orderAdditionalPeriodOneFrom: " + oParams['orderAdditionalPeriodOneFrom']);
    logger.log("rccwf_orderAdditionalPeriodOneTo: " + oParams['orderAdditionalPeriodOneTo']);
    logger.log("rccwf_orderAdditionalPeriodTwoFrom: " + oParams['orderAdditionalPeriodTwoFrom']);
    logger.log("rccwf_orderAdditionalPeriodTwoTo: " + oParams['orderAdditionalPeriodTwoTo']);
    logger.log("rccwf_testing: " + oParams['testing']);

    oParams['title'] = this.title;
    this.args = oParams;
}

customWorkflow.prototype.setTemplate = function() {
    //logger.log("DOCUMENT TEMPLATE");
    if (this.wfId == "vacAppealSimple") {
        if (this.args['wfTitle'] != null && this.args['wfTitle'] != "" && this.args['wfTitle'] != undefined) {
            this.template = companyhome.childByNamePath("/Data Dictionary/Presentation Templates/vacAppealSimple/"+this.args['wfTitle']+".ftl");
            //logger.log("FOUNDED");
        } else {return false;logger.log("NOT FOUNDED");}
    } else if (this.wfId == "orderPerSimple") {
        if (this.args['title'] != null && this.args['title'] != "" && this.args['title'] != undefined) {
            this.template = companyhome.childByNamePath("/Data Dictionary/Presentation Templates/vacAppealSimple/"+this.args['title']+"-order.ftl");
        } else {return false;}
    }
}

customWorkflow.prototype.createDocument = function() {
    //logger.log("CREATE DOCUMENT");
    if (this.template != null) {
        //logger.log("Userhome: " + userhome);
        var folder = userhome.childByNamePath(this.destination);
        if (folder == null ) {
            var folder = userhome.createFolder(this.destination);
        }
        if (folder != null) {
            var outputFile = null;
            if (this.wfId == "vacAppealSimple") {
                outputFile = folder.createFile(this.type+" "+ this.args['wfTitle'] +" "+ initiator.properties.userName + " " + this.dateInDigits+" "+this.dateTime+".html");
            } else if (this.wfId == "orderPerSimple") {
                outputFile = folder.createFile(this.type+" "+ this.args['title'] +" "+ initiator.properties.userName + " " + this.dateInDigits+" "+this.dateTime+".html");

            }
            var result = outputFile.processTemplate(this.template, this.args);
            //logger.log("Output file: " + outputFile);
            outputFile.content = result;
            var newPdf = outputFile.transformDocument("application/pdf", folder);
            if (newPdf.type!="{http://appdevel?.kada.lt/model/content/1.0}veiklosDokumentas") newPdf.specializeType("rcc:veiklosDokumentas");
            outputFile.addAspect("sys:temporary");
            outputFile.remove();
            if(initiator.properties.firstName != "" && initiator.properties.firstName != null && initiator.properties.lastName != "" && initiator.properties.lastName != null){
                var AsmVarPav = initiator.properties.firstName + " " + initiator.properties.lastName;
                newPdf.properties["rcc:dokRenAsmVarPav"] = AsmVarPav;
            } else {
                newPdf.properties["rcc:dokRenAsmVarPav"] = initiator.properties.userName;
            }
            newPdf.properties["rcc:dokRenViet"] = initiator.properties.location;
            newPdf.properties["rcc:dokRenDat"] = this.date;
            newPdf.properties["rcc:stat"] = "Rengiamas";
            newPdf.save();
            bpm_package.addNode(newPdf);
        }
    } else {
        logger.log("CREATE DOCUMENT: FAILED");
        return false;
    }
    logger.log("CREATE DOCUMENT SUCCESS");
}

customWorkflow.prototype.getMonthInText = function(m) {
    var month = "";
    switch (m){
        case 0: month="sausio"; break;
        case 1: month="vasario"; break;
        case 2: month="kovo"; break;
        case 3: month="baland\u017eio"; break;
        case 4: month="gegu\u017e\u0117s"; break;
        case 5: month="bir\u017eelio"; break;
        case 6: month="liepos"; break;
        case 7: month="rugpj\u016b\u010dio"; break;
        case 8: month="rugs\u0117jo"; break;
        case 9: month="spalio"; break;
        case 10: month="lapkri\u010dio"; break;
        case 11: month="gruod\u017eio"; break;
    }

    return month;
}

customWorkflow.prototype.getMonthInDoubleDigits = function(m) {
    var month = "";
    switch (m){
        case 0: month="01"; break;
        case 1: month="02"; break;
        case 2: month="03"; break;
        case 3: month="04"; break;
        case 4: month="05"; break;
        case 5: month="06"; break;
        case 6: month="07"; break;
        case 7: month="08"; break;
        case 8: month="09"; break;
        case 9: month="10"; break;
        case 10: month="11"; break;
        case 11: month="12"; break;
    }

    return month;
}
